package com.zmn.oms.admin.controller.common;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.oms.admin.controller.OmsBaseController;
import com.zmn.oms.business.interfaces.conf.distribute.DistributeWeightListBService;
import com.zmn.oms.common.constant.OrderTagConsts;
import com.zmn.oms.common.dro.conf.channel.ConfOrderChannelDRO;
import com.zmn.oms.common.dro.conf.distribute.DistributeConfigDRO;
import com.zmn.oms.common.dto.ResultDTO;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.common.vo.KVDict;
import com.zmn.oms.model.entity.conf.cancel.OrderCancelReasonQuery;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTag;
import com.zmn.oms.model.entity.conf.ordertag.ConfOrderTagQuery;
import com.zmn.oms.model.vo.conf.cancel.OrderCancelReasonVO;
import com.zmn.oms.model.vo.order.PrepayAmountVO;
import com.zmn.oms.services.interfaces.conf.cancel.OrderCancelReasonService;
import com.zmn.oms.services.interfaces.conf.ordertag.ConfOrderTagService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 类描述：common
 *
 * @author liuying
 * @date 2018/11/21 15:36
 */
@Slf4j
@Controller
@RequestMapping("/common")
public class CommonController extends OmsBaseController {

    @Resource
    private OrderCancelReasonService orderCancelReasonService;
    @Resource
    private ConfOrderTagService confOrderTagService;
    @Resource
    private DistributeWeightListBService distributeWeightListBService;

    /**
     * 工单标记
     */
    @PostMapping(value = "/ordertag/listPage")
    @ResponseBody
    public ResultDTO listTagByQuery(@RequestBody ConfOrderTagQuery query) {
        List<ConfOrderTag> confList = confOrderTagService.listByQuery(query);
        return ResultDTO.success(confList);
    }

    /**
     * 品牌列表
     */
    @GetMapping(value = "/listBrand")
    @ResponseBody
    public ResultDTO listBrand() {
        return ResultDTO.success(super.getBrandList());
    }

    /**
     * 用户层级列表
     */
    @GetMapping(value = "/listUserLevel")
    @ResponseBody
    public ResultDTO listUserLevel() {
        List<KVDict> list = Lists.newArrayList();
        list.add(KVDict.builder().text("要客").value(999).build());
        list.add(KVDict.builder().text("高价值用户").value(OrderTagConsts.TAG_ID_HIGH_VALUE_USER).build());
        return ResultDTO.success(list);
    }

    /**
     * 取消缘由列表
     *
     * @param bizType
     * @return
     */
    @RequestMapping(value = "cancelReason/list", method = RequestMethod.GET)
    @ResponseBody
    public ResultDTO cancelReasonList(int bizType) {
        List<KVDict> collect = Lists.newArrayList();
        OrderCancelReasonQuery orderCancelReasonQuery = new OrderCancelReasonQuery();
        orderCancelReasonQuery.setBizType(bizType);
        orderCancelReasonQuery.setStatus(GlobalConsts.YES);
        List<OrderCancelReasonVO> orderCancelReasonVOList = orderCancelReasonService.listByQuery(orderCancelReasonQuery);
        if (CollectionUtils.isNotEmpty(orderCancelReasonVOList)) {
            orderCancelReasonVOList.forEach(item -> collect.add(KVDict.builder().value(item.getId()).text(item.getReason()).build()));
        }
        return ResultDTO.success(collect);
    }

    @GetMapping("/check/grid")
    @ResponseBody
    public ResultDTO checkGrid(Integer platWork, Integer cityId, Integer bizType) {
        logger.debug("检查是否平台直派入参：platWork={},cityId={},bizType={}", platWork, cityId, bizType);
        DistributeConfigDRO responseDistributeConfigDTO = distributeWeightListBService.findDistributeConfig(platWork, 0, cityId, bizType);
        logger.debug("检查是否平台直派出参:" + responseDistributeConfigDTO);
        if (responseDistributeConfigDTO == null) {
            return ResultDTO.success();
        }
        return ResultDTO.success(responseDistributeConfigDTO.getDistributeWay());
    }

    /**
     * 获取自动派单时间
     *
     * @return
     * @modifier: huangchao 2022/04/01/ 取消自动派单数据逻辑
     */
    @RequestMapping("/getAutoDistributeTime")
    @ResponseBody
    @Deprecated
    public ResultDTO getAutoDistributeTime(Integer platWork, Integer cityId, Integer bizType) {
        /*logger.info("获取自动派单时间入参：platWork={},cityId={},bizType={}", platWork, cityId, bizType);
        DistributeConfigDRO distributeConfigDRO = distributeWeightListBService.findDistributeConfig(platWork, 0, cityId, bizType);
        logger.info("获取自动派单时间出参:" + distributeConfigDRO);
        if (distributeConfigDRO == null) {
            return ResultDTO.success();
        }

        DistributeArgsDRO distributeArgsDRO;
        // PS:网格化差异点
        if (Objects.equals(distributeConfigDRO.getDistributeWay(), OrderDistributeConsts.DISTRIBUTE_WAY_PLAT)) {
            distributeArgsDRO = distributeConfigDRO.getPlatDirectlyDistributeArgsDRO();
        } else {
            distributeArgsDRO = distributeConfigDRO.getDistributeArgsDRO();
        }

        // 1 停用
        if (distributeArgsDRO == null || Objects.equals(distributeArgsDRO.getAutoStatusNew(), 1)) {
            return ResultDTO.success();
        }

        // 默认自动派单起止时间，早八到晚八点
        String autoTimeStart = "8:00";
        String autoTimeEnd = "20:00";

        if (StringUtils.isNotBlank(distributeArgsDRO.getAutoTimeStart()) && StringUtils.isNotBlank(distributeArgsDRO.getAutoTimeEnd())) {
            autoTimeStart = distributeArgsDRO.getAutoTimeStart();
            autoTimeEnd = distributeArgsDRO.getAutoTimeEnd();
        }
        return ResultDTO.success(String.format("%s-%s", autoTimeStart, autoTimeEnd));*/
        return ResultDTO.success();
    }

    /**
     * 查询渠道是否允许改预付款
     * @param channelId
     * @return
     */
    @GetMapping("/getChannelPrepayType")
    @ResponseBody
    public ResultDTO getChannelPrepayType(Integer channelId) {
        ConfOrderChannelDRO confOrderChannelDRO = null;
        try {
            confOrderChannelDRO = confOrderChannelBService.getByChannelId(channelId);
        } catch (OmsBaseException omsBaseException) {
            omsBaseException.printStackTrace();
        }
        log.info("[{}]查询渠道配置出参：{}", channelId, JSONObject.toJSONString(confOrderChannelDRO));
        if (Objects.isNull(confOrderChannelDRO)) {
            return ResultDTO.fail("无法获取渠道信息");
        }
        PrepayAmountVO vo = new PrepayAmountVO();
        vo.setPrepayType(confOrderChannelDRO.getPrepayType());
        //vo.setPrepayType(com.zmn.consts.GlobalConsts.YES);
        //String test = "京豆,东券";
        if (StringUtil.isNotBlank(confOrderChannelDRO.getPrepayAmountDetail())) {
            List<String> strList = StringUtil.splitStrs(confOrderChannelDRO.getPrepayAmountDetail());
            List<KVDict> result = Lists.newArrayListWithCapacity(strList.size());
            for (int i = 0; i < strList.size(); i++) {
                result.add(KVDict.builder().text(strList.get(i)).build());
            }
            vo.setDetailItemList(result);
        }
        return ResultDTO.success(vo);
    }
}
